package org.come.login;

import com.tool.tcp.NewPart;
import com.tool.tcp.SpriteFactory;
import com.tool.tcpimg.UIUtils;
import org.come.bean.ConfigureBean;
import org.come.entity.RoleTableList;
import org.come.entity.RoleTableNew;
import org.come.model.Configure;
import org.come.until.AnalysisString;
import org.come.until.UserMessUntil;
import org.come.view.View;

import javax.swing.*;
import java.awt.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RoleView extends View {

  ConfigureBean allConfigure = UserMessUntil.getConfigureBean();
  Map<BigDecimal, Configure> item = allConfigure.getAllConfigure();
  Configure configure9 = item.get(new BigDecimal(9));

  SpriteBtn fanghui;
  private ImageIcon rolebeijing;
  private SpriteBtn btnEntergame;
  private SpriteBtn btnCreateRole;
  private SpriteBtn[] rolebtn;
  public String ShowRoleNameAndGrade;
  public String ShowRoleGrade;
  public String Grade;
  public String sex;
  public String race;
  public ImageIcon roleImageIcon;
  public Color nameAndGradeColor;
  public Color nameAndGradeColorD;
  private List<JLabel> roles = new ArrayList();
  private RoleTableNew roleTableNew = null;
  private NewPart part;
  private long time;
  // 11返回
  // 选择角色背景
  public RoleTableNew getRoleTableNew() {
    return this.roleTableNew;
  }

  public void setRoleTableNew(RoleTableNew roleTableNew) {
    this.roleTableNew = roleTableNew;
  }

  public RoleView(LoginJpanel loginJpanel) {

    // TODO Auto-generated constructor stub
    this.setBounds(0, 0, 1024, 720);
    rolebeijing = new ImageIcon("resource/NewUi/003.png");

    //this.fanghui = new SpriteBtn("resource/NewUi/返回按钮全新", 10, 680, false);
    //this.fanghui.setBounds(10, 680, 60, 30);
    //this.fanghui.addMouseListener(new RoleMouslisten(11, this.fanghui, loginJpanel));
    //add(this.fanghui);
    // 进入游戏

      btnEntergame = new SpriteBtn("resource/NewUi/进入游戏_按钮", 415, 523, false);
      btnEntergame.setBounds(429, 533, 140, 35);
      btnEntergame.addMouseListener(new RoleMouslisten(12, btnEntergame,loginJpanel));
      this.add(btnEntergame);
    rolebtn = new SpriteBtn[5];
    // 创建新角色
    btnCreateRole = new SpriteBtn("resource/NewUi/创建新角色", 744, 34,false);
    btnCreateRole.setBounds(744, 34, 245, 73);
    btnCreateRole.addMouseListener(new RoleMouslisten(13, btnCreateRole,loginJpanel));
    this.add(btnCreateRole);


    sx(loginJpanel.getRoleArr(), loginJpanel);
  }

  public void sx(RoleTableList roleArr, LoginJpanel loginJpanel) {
    if (roleArr != null && roleArr.getRoleList() != null) {
      for (int i = 0; i < roleArr.getRoleList().size(); i++) {
        RoleTableNew roleTableNew = roleArr.getRoleList().get(i);
        if (rolebtn[i]==null) {
          rolebtn[i] = new SpriteBtn("resource/NewRoleUi/36角色/角色按钮_"+ roleTableNew.getLocalname(), 1024, (200 + i * 75),true, 1, 120, 120);
          rolebtn[i].setBounds(780, (120 + i * 75), 245, 73);
          rolebtn[i].addMouseListener(new RoleMouslisten(14 + i,rolebtn[i], loginJpanel));
        }else {
          rolebtn[i].setSprite("resource/NewRoleUi/36角色/角色按钮_"+ roleTableNew.getLocalname());
        }
        this.add(rolebtn[i]);
        rolebtn[0].btn(2);
        if (i == 0) {
          // 等级// 角色名// 颜色
          ShowRoleGrade = AnalysisString.lvl(roleTableNew.getGrade());
          ShowRoleNameAndGrade = roleTableNew.getRolename();
          nameAndGradeColor = UIUtils.getcolor(roleTableNew.getTurnaround());
          nameAndGradeColorD = UIUtils.getDarkColor(roleTableNew.getTurnaround());
          part=SpriteFactory.createPart(roleTableNew.getSpecies_id().toString(), 2, 1, null);
        }
      }

        btnCreateRole.setSy(roleArr.getRoleList().size() * 75 + 134);
        btnCreateRole.setBounds(780, (roleArr.getRoleList().size() * 75 + 134),245, 73);

    }else {

        btnCreateRole.setBounds(780, 34, 245, 73);

    }
    for (int i = (roleArr != null && roleArr.getRoleList() != null)?roleArr.getRoleList().size():0; i < rolebtn.length; i++) {
      if (rolebtn[i]!=null) {
        remove(rolebtn[i]);
        rolebtn[i]=null;
      }
    }
  }

  public void xz(RoleTableNew login, int i) {
    //取消选中的
    LoginJpanel.quxiao(rolebtn, i);
    ShowRoleGrade = AnalysisString.lvl(login.getGrade());
    ShowRoleNameAndGrade = login.getRolename();
    nameAndGradeColor = UIUtils.getcolor(login.getTurnaround());
    nameAndGradeColorD = UIUtils.getDarkColor(login.getTurnaround());
    part=SpriteFactory.createPart(login.getSpecies_id().toString(), 2, 1, null);
  }

  @Override
  protected void paintComponent(Graphics g) {
    // TODO Auto-generated method stub
    super.paintComponent(g);
    g.drawImage(rolebeijing.getImage(), 0, 0, 1024, 720, null);

      btnEntergame.draw(g);
      btnCreateRole.draw(g);

    for (int i = 0; i < rolebtn.length; i++) {
      if (rolebtn[i] != null) {
        rolebtn[i].draw(g);
      }
    }
    // 返回按钮
    // fanghui.draw(g);
    if (part!=null) {
      time+=30;
      part.draw(g, 510, 470, 4, time);
//      Font f5 = UIUtils.nameFont;
//      g.setFont(f5);
//      // 设置角色名字// 设置等级
//      // 根据名字长度计算位置
      int middle = 350; //中心点
      int size = 4;//一个字节占10像素
      int nameX = middle - String_length(ShowRoleNameAndGrade) * size;
      int levelX = middle - String_length(ShowRoleNameAndGrade+"级") * size;
//      g.setColor(UIUtils.COLOR_NAME16);
//      g.drawString(ShowRoleNameAndGrade, nameX + 150, 498);
//      g.setColor(nameAndGradeColorD);
//      g.drawString(ShowRoleNameAndGrade, nameX + 150, 498);
//      g.setColor(nameAndGradeColor);
//      g.drawString(ShowRoleNameAndGrade, nameX + 150, 498);
//      g.setColor(UIUtils.COLOR_NAME16);
//      g.drawString(ShowRoleGrade, levelX + 156, 518);
//      g.setColor(nameAndGradeColorD);
//      g.drawString(ShowRoleGrade, levelX + 156, 518);
//      g.setColor(nameAndGradeColor);
//      g.drawString(ShowRoleGrade, levelX + 156, 518);


//      part.draw(g, 505, 475, 4, time);
//      Font f5 = new Font("微软雅黑", Font.PLAIN, 20);
      g.setFont(UIUtils.TEXT_MSG17);
      g.setColor(new Color(0, 0, 0, 180));
//      g.drawString(ShowRoleNameAndGrade, 483, 505);
//      g.drawString(ShowRoleGrade+"级", levelX+135, 525);

      g.setColor(nameAndGradeColor);
      // 设置角色名字// 设置等级
      g.drawString(ShowRoleNameAndGrade, 483, 505);
      if(ShowRoleGrade.contains("转")){
        g.drawString(ShowRoleGrade+"级", levelX+150, 525);

      }else{

        g.drawString(ShowRoleGrade+"级", levelX+135, 525);
      }
    }
  }

  private String getRace(String raceId) {
    if ("10001".equals(raceId)) {
      return "人";
    }
    if ("10002".equals(raceId)) {
      return "魔";
    }
    if ("10003".equals(raceId)) {
      return "仙";
    }
    return "鬼";
  }
  public void setRoleInfo(String grade, String sex, String race, Color color) {
    this.Grade = grade;
    this.nameAndGradeColor = color;
    this.nameAndGradeColorD = color;
    this.sex = sex;
    this.race = getRace(race);
  }

  void clearRoleInfo() {
    this.Grade = "";
    this.sex = "";
    this.race = "";
  }

  public static int String_length(String value) {
    int valueLength = 0;
    String chinese = "[-]";
    for (int i = 0; i < value.length(); i++) {
      String temp = value.substring(i, i + 1);
      if (temp.matches(chinese)) {
        valueLength += 2;
      } else {
        valueLength++;
      }
    }
    return valueLength;
  }

}
